/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.jctc.els.modules.biz.controller;


import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.jctc.els.modules.auth.provider.ITokenGranter;
import org.jctc.els.modules.auth.provider.TokenGranterBuilder;
import org.jctc.els.modules.auth.provider.TokenParameter;
import org.jctc.els.modules.auth.utils.TokenUtil;
import org.jctc.els.modules.biz.entity.UserInfo;
import org.jctc.els.modules.biz.entity.user.Student;
import org.jctc.els.modules.biz.entity.user.UserExpandVO;
import org.jctc.els.modules.biz.service.user.IUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.redis.cache.BladeRedis;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tenant.annotation.NonDS;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.support.Kv;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.RandomType;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.core.tool.utils.WebUtil;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * 登录相关的接口
 *
 * @author Rxy
 */
@NonDS
@ApiIgnore
@RestController
@RequestMapping("/login")
@AllArgsConstructor
public class LoginController extends BladeController {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	private final BladeRedis bladeRedis;

	private final IUserService userService;

	/**
	 * 获取短信验证码的接口
	 */
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "获取短信验证码的接口", notes = "")
	@GetMapping("/send-code")
	public R sendSmsCode(@RequestParam("mobile") String mobile) {
		String random = StringUtil.random(6, RandomType.INT);
		//保存验证码，有效时间为5分钟
		bladeRedis.setEx(this.buildSmsCodeKey(mobile),random,60*5L);
		logger.error("验证码："+random);
		return R.data(random);
	}

	/**
	 * 校验手机短信验证码，并获取学生列表
	 * @return
	 */
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "获取短信验证码的接口", notes = "")
	@PostMapping("/check-mobile")
	public R checkSmsCode(@RequestBody Kv requestParams){
		String mobile =  requestParams.get("mobile").toString();
		String smsCode =  requestParams.get("code").toString();
		//校验验证码
		String sessionCode = bladeRedis.get(this.buildSmsCodeKey(mobile));
		bladeRedis.del(this.buildSmsCodeKey(mobile));
		if(!smsCode.equals(sessionCode)){
			return R.fail("验证码错误");
		}else{
			return R.success("操作成功");
		}
	}

	private String buildSmsCodeKey(String mobile){
		return mobile + "SMSCODE";
	}





}
